package com.sls.web.manager.recharge.service.impl;

import java.lang.reflect.Type;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.CardStatistics;
import com.sls.web.manager.recharge.querybean.CardStatisticsQuery;
import com.sls.web.manager.recharge.service.CardStatisticsService;
import com.sls.web.manager.socket.SuperService;

/**
 * 卡密统计信息维护
 * 
 * @author sxf
 * 
 */
@Service("cardStatisticsService")
public class CardStatisticsServiceImpl extends SuperService implements
		CardStatisticsService {
	private static Logger log = Logger
			.getLogger(CardStatisticsServiceImpl.class);

	/**
	 * @param serviceCode
	 * @param sendObj
	 * @param userInfoVO
	 * @param classType
	 *            类型信息(如:String,为String.class)
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public <T> T callService(String serviceCode, Head head, Object sendObj,
			UserInfoVO userInfoVO, Object... classType) throws Exception {
		Gson gson = CommonUtil.gson();
		if (head == null) {
			head = new Head();
			head.setRequestId(UUID.randomUUID().toString());
			head.setServiceCode(serviceCode);
			head.setIp(userInfoVO.getCurrentIp());
			head.setUserId(userInfoVO.getUserId());
		}
		SocketDTO requestDTO = super.send(head, sendObj);
		if (!TransmissionCode.SUCCESS.getCode().equals(
				requestDTO.getHead().getResponseCode())) {
			throw new ManagerException(requestDTO.getHead()
					.getResponseMessage());
		}
		String body = requestDTO.getBody();
		T t = null;
		if (body != null) {
			if (classType != null && classType.length > 0) {
				if (classType[0] instanceof Class) {
					t = gson.fromJson(body, (Class<T>) classType[0]);
				} else if (classType[0] instanceof Type) {
					t = gson.fromJson(body, (Type) classType[0]);
				}
			}
		}
		return t;
	}

	public boolean insert(CardStatistics info, UserInfoVO userInfoVO)
			throws Exception {
		this.callService(Constants.INSERT_CARDSTORESTATISTICS, null, info,
				userInfoVO);
		return true;
	}

	public Pagination<CardStatistics> queryPage(CardStatisticsQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter CardStoreStatisticsService queryPage method...");
		Pagination<CardStatistics> infoPage = callService(
				Constants.QUERY_CARDSTORESTATISTICS_PAGE, null, query,
				userInfoVO, new TypeToken<Pagination<CardStatistics>>() {
				}.getType());
		log.info("Leave CardStoreStatisticsService queryPage method...");
		return infoPage;
	}

	public CardStatistics queryOne(CardStatisticsQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter CardStoreStatisticsService queryPage method...");
		CardStatistics infoPage = callService(
				Constants.QUERY_CARDSTORESTATISTICS_ONE, null, query,
				userInfoVO, CardStatistics.class);
		log.info("Leave CardStoreStatisticsService queryPage method...");
		return infoPage;
	}

	public CardStatistics queryStatistics(String systemProductId,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter CardStoreStatisticsService queryPage method...");
		CardStatistics infoPage = callService(
				Constants.QUERY_CARDSTORESTATISTICS_STATISTICS, null,
				systemProductId, userInfoVO, CardStatistics.class);
		log.info("Leave CardStoreStatisticsService queryPage method...");
		return infoPage;
	}
}
